package com.aischool.service;

import com.aischool.utils.RegexUtil;
import com.aischool.utils.StringUtil;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;

public class AiAppArticleService {

	public boolean updateAppAtricle(int app_id, int org_id, String ids, int use_state) {
		if (StrKit.isBlank(ids) || !RegexUtil.isSqlIds(ids)) {
			return false;
		}

		int ir = 0;
		ir = Db.delete("DELETE FROM ai_app_article a " + 
				"USING (SELECT regexp_split_to_table(COALESCE(?,''),E',') AS ids) AS tt " +
				"WHERE a.app_id = ? AND a.org_id = ? AND a.article_id = CAST(tt.ids AS INTEGER)", 
				StringUtil.trim(ids, ','), app_id, org_id);
		if (use_state == 0) {
			ir = Db.update("INSERT INTO ai_app_article(app_id, org_id, article_id, use_state) " + 
					"SELECT ?, ?, CAST(tt.ids AS INTEGER), 1 " + 
					"FROM (SELECT regexp_split_to_table(COALESCE(?,''),E',') AS ids) AS tt",
					app_id, org_id, StringUtil.trim(ids, ','));
		}
		return ir > 0;
	}
}
